<?php
namespace app\controller;

use think\Controller;
use Overtrue\PHPOpenCC\OpenCC;

class Publishdynamic extends Controller
{
    private $_url = [
        
        ['https://pcwallpaper.zhhainiao.com/mobile/wallpaper/live/list',
        '{"login_info":{},"cate_id":2,"page":%page%,"page_size":24,"common":{"open_id":"T4cdd25b345164816682c5d350004e4dd","token":"PoaKVlwLkVrRDmBeHayuVZvPew7pwGIXED9m3peh","device_id":"7f18c640d5cb6c80f8eb8cf1569f7f9a","player_version":115,"platform":"pc"}}',
        1],
       
    ];

    public function run_index($index){
		$this->_run($this->_url[$index]);
	}

    private function _run($current){

		$page = 1;
        $spider_source = 'zhhainiao';
		while (1) {
			echo "page {$page} cate_id {$current[2]}\n";
			$url = $current[0];
			$post_base = $current[1];
			$cate_id = $current[2];
			$post = str_replace("%page%", $page, $post_base);
			$contents = curl_post($url,$post);
			$contents = json_decode($contents,true);
			if (empty($contents['data']['list'])) {
				echo "没有数据\n";
				break;
			}
			foreach ($contents['data']['list'] as $value) {
				$title = $value['wname'];
				if (empty($title)) {
					$title = str_replace('#', '', $value['tags_str']);
				}
                $title = str_replace([',','，'], ' ', $title);
				$title = trim($title);
				if (empty($title)) {
					continue;
				}
				$spider_id = $value['wallpaper_id'];
				$exits = db('wallpaper_image')
                    ->where([
                        'spider_id'=>$spider_id,
                        'spider_source'=>$spider_source])
                    ->find();
				//var_dump($spider_id,$exits);exit;
				
				if (!empty($exits)) {
					echo "title {$title} repeat\n";
					continue;
				}
				$title = openCC::simplifiedToTaiwan($title);

				$small_jpg_url = $value['mobile_preview_jpg'];
                $mid_jpg_url = $value['mobile_preview_mid_jpg'];
                $mp4_preview = $value['mobile_preview_video'];
                $mp4 = $value['mobile_video'];
				
				$local_small_jpg = download($small_jpg_url);
				$local_mid_jpg = download($mid_jpg_url);
				$local_mp4_preview = download($mp4_preview);
				$local_mp4 = download($mp4);

                if (empty($local_small_jpg) && empty($local_mid_jpg) && empty($local_mp4_preview) && empty($local_mp4)){
                    continue;
                }
                $cover_url = $local_small_jpg ? $local_small_jpg : ($local_mid_jpg ? $local_mid_jpg : '');
				
				$pic = [
                    'small_jpg'	=> $local_small_jpg,
                    'mid_jpg'	=> $local_mid_jpg,
                    'mp4_preview'	=> $local_mp4_preview,
                    'mp4'	=> $local_mp4,
                ];

                $value['tags_str'] = explode(',', $value['tags_str']);
                $value['tags_str'] = array_unique($value['tags_str']);
                $tag_name = implode(',', $value['tags_str']);
				
                $title_simplify = filter_non_text_chars($title);

                $wallpaper_title_data = db('wallpaper_title')
                                        ->where(['title_simplify'=>$title_simplify])
                                        ->where('type','dynamic_mobile')
                                        ->find();
                // debug_dump($wallpaper_title_data);
                if (empty($wallpaper_title_data)) {
                    $id = db('wallpaper_title')->insertGetId([
                        'title'         => $title,
                        'title_simplify'=> $title_simplify,
                        'type'          => 'dynamic_mobile',
                        'category_id'   => $cate_id,
                        'cover_url'     => $cover_url,
                        'tag_name'      => $tag_name,
                        'create_time'   => date('Y-m-d H:i:s'),
                        'update_time'   => date('Y-m-d H:i:s'),
                    ]);
                }else{
                    $id = $wallpaper_title_data['id'];
                }
				if (!$id){
                    echo "title {$title} insert failed\n";
                    exit();
                }
                $wallpaper_image_id = db('wallpaper_image')->insert([
                    'title_id'      => $id,
                    'file_path'     => json_encode($pic,JSON_UNESCAPED_UNICODE),
                    'create_time'   => date('Y-m-d H:i:s'),
                    'update_time'   => date('Y-m-d H:i:s'),
                    'spider_source' => $spider_source,
                    'spider_id'     => $spider_id,
                ]);
                if (!$wallpaper_image_id){
                    echo "title {$title} insert failed\n";
                    exit();
                }
                if ($wallpaper_title_data){
                    $db_tag_name = explode(',', $wallpaper_title_data['tag_name']);
                    $tag_name = explode(',', $tag_name);
                    if (empty($db_tag_name)){
                        $db_tag_name = [];
                    }
                    if (empty($tag_name)){
                        $tag_name = [];
                    }
                    $tag_name = array_merge($db_tag_name, $tag_name);
                    $tag_name = array_unique($tag_name);
                    $tag_name = implode(',', $tag_name);
                    db('wallpaper_title')->where(['id'=>$id])->update([
                        'cover_url'     => $cover_url,
                        'tag_name'      => $tag_name,
                        'update_time'   => date('Y-m-d H:i:s'),
                    ]);
                }
				echo "title {$title} success\n";
				exit;    
			}
			++$page;
		}
	}
}